package com.example.unisys.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.unisys.annotation.Log;
import com.example.unisys.cache.UserManager;
import com.example.unisys.common.Response;
import com.example.unisys.common.STATUS;
import com.example.unisys.entity.*;
import com.example.unisys.form.BigStudentForm;
import com.example.unisys.form.ExaminationForm;
import com.example.unisys.service.*;
import com.example.unisys.utils.DateUtil;
import com.example.unisys.utils.Quiz2JsonUtil;
import com.example.unisys.utils.BaseUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping(value = "/apis/v1/system", produces = "application/json; charset=utf-8")
public class SystemController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseSaleService courseSaleService;
    @Autowired
    private StuCourseService stuCourseService;
    @Autowired
    private TagService tagService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private ExaminationService examinationService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserManager userManager;
    @Autowired
    private StuExamService stuExamService;


    /*******************************课程中心API**************************************************/
    /**
     * 获取仓库课程
     *
     * @param limit
     * @param page
     * @param searchType
     * @param searchText
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/courses", method = RequestMethod.GET)
    public Response getSystemCourses(@RequestParam("limit") int limit, @RequestParam("page") int page,
                                     @RequestParam("searchType") int searchType, @RequestParam("searchText") String searchText) {
        HashMap<String, Object> hs = new HashMap<>();
        int start = (page - 1) * limit;
        hs.put("start", start);
        hs.put("pagesize", limit);
        /*模糊查询*/
        hs.put("searchText", searchText.trim());
        List<Course> courses = courseService.getAllRepoCourses(hs);
        int total = courseService.getRepoCourseSum();
        return Response.builder().data(courses).status(STATUS.SUCCESS).total(total).build();
    }


    /**
     * 创建仓库课程
     *
     * @param jsonParam
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/courses", method = RequestMethod.POST)
    public Response createRepoCourse(@RequestBody JSONObject jsonParam) {
        Course course = JSONObject.parseObject(jsonParam.toJSONString(), Course.class);
        System.out.println("course:" + course.toString());
        courseService.save(course);
        return Response.builder().status(STATUS.SUCCESS).build();

    }


    /**
     * 更新仓库课程
     *
     * @param jsonParam
     * @return
     */
    @Log("更新仓库课程信息")
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/courses", method = RequestMethod.PUT)
    public Response updateRepoCourse(@RequestBody JSONObject jsonParam) {
        Course course = JSONObject.parseObject(jsonParam.toJSONString(), Course.class);
        courseService.update(course);
        return Response.builder().status(STATUS.SUCCESS).build();

    }


    /**
     * 删除仓库课程
     * danger func
     *
     * @param course_id
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/courses/{course_id}", method = RequestMethod.DELETE)
    public Response deleteSystemCourse(@PathVariable("course_id") String course_id) {
        boolean status = courseService.remove(new QueryWrapper<Course>().lambda().eq(Course::getCourse_id, course_id));
        if (status)
            return Response.builder().status(STATUS.SUCCESS).message("删除成功").build();
        return Response.builder().status(STATUS.BAD_REQUEST).message("删除失败").build();
    }


    /**
     * 上架课程，不需要测验，采用了新的逻辑
     *
     * @param jsonParam
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/courses/publish", method = RequestMethod.POST)
    @Log("上架仓库课程")
    public Response publishRepoCourse(@RequestBody JSONObject jsonParam) {
        Course course = JSONObject.parseObject(jsonParam.toJSONString(), Course.class);
        int status = courseService.update(course);
        if (status > 0) return Response.builder().status(STATUS.SUCCESS).message("更新课程信息成功").build();
        return Response.builder().status(STATUS.BAD_REQUEST).message("课程尚未完善，请检查，课程名，课程目录是否为空").build();
//        if (courseService.isPublishable(course))
//            courseService.update(course);
//        else {
//            return Response.builder().status(STATUS.BAD_REQUEST).message("课程尚未完善，请检查，课程名，课程目录，各项测验是否为空").build();
//        }
//        return Response.builder().status(STATUS.SUCCESS).build();
    }


    /**
     * 获取课程，实际上是只需要课程目录树
     *
     * @param course_id
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/courses/{course_id}", method = RequestMethod.GET)
    public Response getCatalogTreeById(@PathVariable("course_id") String course_id) {
        Course course = courseService.getOne(new QueryWrapper<Course>().lambda().eq(Course::getCourse_id, course_id));
        return Response.builder().status(STATUS.SUCCESS).data(course).build();
    }


    /**
     * 获取销量
     *
     * @param limit
     * @param page
     * @param searchText
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/sales", method = RequestMethod.GET)
    public Response getSystemSales(@RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("searchText") String searchText) {
        HashMap<String, Object> hs = new HashMap<>();
        int start = (page - 1) * limit;
        int total;
        hs.put("start", start);
        hs.put("pagesize", limit);
        if (!searchText.equals("")) {
            hs.put("course_id", searchText.trim());
            hs.put("course_name", searchText.trim());
            hs.put("main_school", searchText.trim());
            hs.put("sub_school", searchText.trim());
        }
        Tag tag = tagService.getActiveTag();
        hs.put("tag_name", tag.getTag_name());
        List<CourseSale> results = courseSaleService.getSalesBlur(hs);
        total = (!searchText.equals("")) ? results.size() : courseSaleService.getSystemSaleSum();
        return Response.builder().status(STATUS.SUCCESS).data(results).total(total).build();
    }


    /***************************考期中心API*****************************************************************/
    /**
     * 获取考期列表在TagController
     */

    /**
     * 创建考期
     *
     * @param jsonParam
     * @return
     */
    @Log("创建考期")
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/tags", method = RequestMethod.POST)
    public Response createTag(@RequestBody JSONObject jsonParam) {
        try {
            //插入当前考期
            Tag tag = JSONObject.parseObject(jsonParam.toJSONString(), Tag.class);
            tag.setActive(1);
            tag.setTag_createtime(DateUtil.getCurrentTime());
            tagService.save(tag);
            //让上个考期失效
            HashMap<String, Object> hs = new HashMap<>();
            hs.put("tag_id", tag.getTag_id());
            tagService.setTagActiveById(hs);
            //更新系统数据
            tagService.updateSystemData(hs);
            return Response.builder().status(STATUS.SUCCESS).build();
        } catch (Exception e) {
            return Response.builder().status(STATUS.INTERNAL_SERVER_ERROR).message("系统内部错误：" + e.getMessage()).build();
        }
    }


    /**
     * 修改考期
     *
     * @param jsonParam
     * @return
     */
    @Log("修改考期")
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/tags", method = RequestMethod.PUT)
    public Response updateTag(@RequestBody JSONObject jsonParam) {

        Tag tag = JSONObject.parseObject(jsonParam.toJSONString(), Tag.class);
        tagService.update(tag);
        return Response.builder().status(STATUS.SUCCESS).build();
    }

    /*
     * 删除考期
     * */

    /**
     * 删除考期 并不删除考期学生数据，只删除考期
     *
     * @param tag_id
     * @return
     */
    @Log("删除考期")
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/tags/{tag_id}", method = RequestMethod.DELETE)
    public Response deleteTag(@PathVariable("tag_id") Integer tag_id) {
//        HashMap<String, Object> hs = new HashMap<>();
//        hs.put("tag_id", tag_id);
//        tagService.deleteTagById(hs);
//       boolean status= tagService.remove(new LambdaQueryWrapper<Tag>()
//                .eq(Tag::getTag_id,tag_id));
        boolean status=tagService.removeById(tag_id);
        if (status)
            return Response.builder().status(STATUS.SUCCESS).message("删除成功").build();
        return Response.builder().status(STATUS.BAD_REQUEST).message("删除失败").build();

    }

    /***************************考期中心API*****************************************************************/


    /*************************系统界面学校管理API**************************************************************/
    /**
     * 获取系统学校
     *
     * @param limit
     * @param page
     * @param searchType
     * @param searchText
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/schools", method = RequestMethod.GET)
    public Response getSystemSchools(@RequestParam("limit") int limit, @RequestParam("page") int page,
                                     @RequestParam("searchType") int searchType, @RequestParam("searchText") String searchText) {
        HashMap<String, Object> hs = new HashMap<>();
        int start = (page - 1) * limit;
        int total;
        hs.put("start", start);
        hs.put("pagesize", limit);
        if (!searchText.equals("")) {
            if (BaseUtils.isNumeric(searchText))
                hs.put("school_id", Integer.parseInt(searchText));
            hs.put("title", searchText.trim());
        }
        List<School> schools = schoolService.getAllSchools(hs);
        total = (!searchText.equals("")) ? schools.size() : schoolService.getAllSchoolSum();
        return Response.builder().status(STATUS.SUCCESS).data(schools).total(total).build();
    }

    /**
     * 创建学校
     *
     * @param jsonObject
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/schools", method = RequestMethod.POST)
    public Response createSystemSchool(@RequestBody JSONObject jsonObject) {
        School school = JSONObject.parseObject(jsonObject.toJSONString(), School.class);
        try {
            schoolService.save(school);
            return Response.builder().status(STATUS.SUCCESS).message("新建学校成功").build();
        } catch (Exception e) {
            return Response.builder().status(STATUS.BAD_REQUEST).message("新建学校失败，该学校已经存在，" +
                    "请检测学校名称字段，主校名字段和分校名字段").build();
        }
    }


    /**
     * 修改学校
     *
     * @param jsonObject
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/schools", method = RequestMethod.PUT)
    public Response modifySystemSchool(@RequestBody JSONObject jsonObject) {
        School school = JSONObject.parseObject(jsonObject.toJSONString(), School.class);
        boolean status = schoolService.updateById(school);
        if (status)
            return Response.builder().status(STATUS.SUCCESS).message("修改成功").build();
        return Response.builder().status(STATUS.BAD_REQUEST).message("修改失败，请检测参数").build();
    }

    /**
     * 删除学校
     *
     * @param school_id
     * @return
     */
    @Log("删除学校")
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/schools/{school_id}", method = RequestMethod.DELETE)
    public Response deleteSystemSchool(@PathVariable("school_id") Integer school_id) {
        boolean status = schoolService.removeById(school_id);
        if (status)
            return Response.builder().status(STATUS.SUCCESS).message("删除成功").build();
        return Response.builder().status(STATUS.BAD_REQUEST).message("删除失败").build();

    }

    /**
     * 添加获取主校与分校接口
     *
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/main_schools", method = RequestMethod.GET)
    public Response getMainSchools() {
        ArrayList<String> main_schools = schoolService.getMainSchools();
        if (main_schools.size() > 0)
            return Response.builder().status(STATUS.SUCCESS).data(main_schools).build();
        return Response.builder().status(STATUS.SUCCESS).data(new ArrayList<>()).build();
    }


    /**
     * *获取分校
     *
     * @param main_school
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/sub_schools", method = RequestMethod.GET)
    public Response getMainSchools(@RequestParam("main_school") String main_school) {

        if (main_school.equals(""))
            return Response.builder().status(STATUS.SUCCESS).data(new ArrayList<>()).build();
        ArrayList<String> sub_schools = schoolService.getSubSchools(main_school);
        if (sub_schools.size() > 0)
            return Response.builder().status(STATUS.SUCCESS).data(sub_schools).build();
        return Response.builder().status(STATUS.SUCCESS).data(new ArrayList<>()).build();

    }
    /*************************系统界面学校管理API**************************************************************/







    /*****************************试卷管理界面API**********************************************************/


    /**
     * 获取试卷
     *
     * @param limit
     * @param page
     * @param searchType
     * @param searchText
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/exams", method = RequestMethod.GET)
    public Response getTests(@RequestParam("limit") int limit, @RequestParam("page") int page,
                             @RequestParam("searchType") int searchType, @RequestParam("searchText") String searchText) {
        HashMap<String, Object> hs = new HashMap<>();
        int start = (page - 1) * limit;
        int total;
        hs.put("start", start);
        hs.put("pagesize", limit);
        if (!searchText.equals("")) {
            if (BaseUtils.isNumeric(searchText)) {
                hs.put("examination_id", Integer.parseInt(searchText));
            }
            hs.put("course_name", searchText.trim());
            hs.put("course_id", searchText.trim());
            hs.put("exam_title", searchText.trim());
        }
        List<ExaminationForm> examinations = examinationService.getAllExams(hs);
        total = (!searchText.equals("")) ? examinations.size() : examinationService.getExamSum();
        return Response.builder().status(STATUS.SUCCESS).total(total).data(examinations).build();
    }


    /**
     * 创建试卷
     * 这里返回测验表单
     *
     * @param jsonObject
     * @return
     */
    @Log("创建试卷")
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/exams", method = RequestMethod.POST)
    public Response createTest(@RequestBody JSONObject jsonObject) {
        Examination examination = JSONObject.parseObject(jsonObject.toJSONString(), Examination.class);
        System.out.println(examination);
        try {
            boolean status = examinationService.save(examination);
            if (status)
                return Response.builder().status(STATUS.SUCCESS).message("创建试卷成功").build();
            else
                return Response.builder().status(STATUS.BAD_REQUEST).message("创建试卷失败").build();
        }catch (Exception e){
            return Response.builder().status(STATUS.INTERNAL_SERVER_ERROR).message("请确保课程-试卷类型唯一").build();
        }


    }

    /**
     * 修改试卷
     *
     * @param jsonObject
     * @return
     */
    @Log("修改试卷")
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/exams", method = RequestMethod.PUT)
    public Response modifyTest(@RequestBody JSONObject jsonObject) {
        Examination examination = JSONObject.parseObject(jsonObject.toJSONString(), Examination.class);
        boolean status = examinationService.updateById(examination);
        if (status)
            return Response.builder().status(STATUS.SUCCESS).message("更新试卷成功").build();
        else
            return Response.builder().status(STATUS.BAD_REQUEST).message("更新试卷失败").build();
    }

    /**
     * 根据id获取测验
     *
     * @param examination_id
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/exams/{examination_id}", method = RequestMethod.GET)
    public Response getTestById(@PathVariable("examination_id") Integer examination_id) {
        Examination examination = examinationService.getOne(new LambdaQueryWrapper<Examination>()
                .eq(Examination::getExamination_id, examination_id));
        return Response.builder().status(STATUS.SUCCESS).data(examination).build();

    }


    /**
     * 删除试卷
     *
     * @param examination_id
     * @return
     */
    @Log("删除试卷")
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/exams/{examination_id}", method = RequestMethod.DELETE)
    public Response deleteTest(@PathVariable("examination_id") Integer examination_id) {
//        boolean status = examinationService.remove(new LambdaQueryWrapper<Examination>()
//                .eq(Examination::getExamination_id,examination_id));
        boolean status=examinationService.removeById(examination_id);
        if (status)
            return Response.builder().status(STATUS.SUCCESS).message("删除试卷成功").build();
        else
            return Response.builder().status(STATUS.BAD_REQUEST).message("删除试卷失败").build();

    }
    /*************************试卷中心API*************************************************/


    /*************************设置中心API*************************************************/

    /**
     * 管理员密码查询
     *
     * @param username
     * @return
     */
    @RequiresPermissions("system_entry")
    @RequestMapping(value = "/admins/password", method = RequestMethod.GET)
    public Response getAdminPWD(@RequestParam("username") String username) {
        try {
            String pwd = userManager.getUser(username).getPassword();
            return Response.builder().status(STATUS.SUCCESS).data(pwd).build();
        } catch (Exception e) {
            return Response.builder().status(STATUS.BAD_REQUEST).message("查询的管理员不存在").build();
        }
    }


    /**
     * 管理员列表获取
     *
     * @param limit
     * @param page
     * @param searchText
     * @return
     */
    @RequestMapping(value = "/admins", method = RequestMethod.GET)
    public Response getSystemAdmins(@RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("searchText") String searchText) {
        HashMap<String, Object> response = new HashMap<>();
        HashMap<String, Object> hs = new HashMap<>();
        int start = (page - 1) * limit;
        int total;
        hs.put("start", start);
        hs.put("pagesize", limit);
        if (!searchText.equals("")) {
            hs.put("user_id", searchText.trim());
            hs.put("username", searchText.trim());
            hs.put("main_school", searchText.trim());
            hs.put("sub_school", searchText.trim());
        }
        List<User> results = userService.getAdmins(hs);
        total = (!searchText.equals("")) ? results.size() : userService.getAdminSumBySystem(hs);
        return Response.builder().status(STATUS.SUCCESS).data(results).total(total).build();
    }


    /**
     * 创建管理员
     * 虽然前端已经对字段进行了校验，但是为了避免这种情况出现，还是加一层判断学校是否存在
     *
     * @param jsonObject
     * @return
     */
    @Log("创建管理员接口")
    @RequestMapping(value = "/admins", method = RequestMethod.POST)
    public Response createSystemAdmins(@RequestBody JSONObject jsonObject) {
        User user = JSONObject.parseObject(jsonObject.toJSONString(), User.class);
        if (schoolService.getSchool(user.getMain_school(), user.getSub_school()) != null || user.getUser_type() == 1)
            return Response.builder().status(STATUS.SUCCESS).data(userService.insert(user)).build();
        return Response.builder().status(STATUS.BAD_REQUEST).data("无此学校，无法创建管理员").build();
    }

    /**
     * 更新管理员信息
     * 管理员前端有两处调用此接口，管理自己信息和系统管理员更新自己信息
     */
    @Log("更新信息接口")
    @RequestMapping(value = "/admins", method = RequestMethod.PUT)
    public Response updateSystemAdmins(@RequestBody JSONObject jsonObject) {
        User user = JSONObject.parseObject(jsonObject.toJSONString(), User.class);
        boolean status = userManager.update(user);
        if (status)
            return Response.builder().status(STATUS.SUCCESS).message("更新信息成功").build();
        return Response.builder().status(STATUS.BAD_REQUEST).message("更新信息失败").build();
    }

    /**
     * 查询学生密码
     *
     * @param searchText
     * @return
     */
    @RequestMapping(value = "/students/password", method = RequestMethod.GET)
    public Response searchStudentPassword(@RequestParam("searchText") String searchText) {
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("id_card", searchText.trim());
        hs.put("exam_id", searchText.trim());
        Student student = studentService.searchStudent(hs);
        if (null != student)
            return Response.builder().status(STATUS.SUCCESS).data(student).build();
        else
            return Response.builder().status(STATUS.BAD_REQUEST).message("无此考生").data(null).build();
    }
    /*************************设置中心API*************************************************/


    /*************************督学列表API*************************************************/

    /**
     * 督学列表 系统部分
     *
     * @param limit
     * @param page
     * @param start_date
     * @param end_date
     * @return
     */
    @RequestMapping(value = "/supervise/students", method = RequestMethod.GET)
    public Response getSupervise(@RequestParam("limit") int limit, @RequestParam("page") int page,
                                 @RequestParam("start_date") String start_date,
                                 @RequestParam("end_date") String end_date) {
        HashMap<String, Object> hs = new HashMap<>();
        int start = (page - 1) * limit;
        hs.put("start", start);
        hs.put("pagesize", limit);
        if (start_date.equals("") || end_date.equals("")) ;
        else {
            hs.put("start_date", start_date);
            hs.put("end_date", end_date);
        }
//        if (main_school.equals("")) ;
//        else if (main_school.trim().equals(sub_school.trim())) {
//            hs.put("main_school", main_school.trim());
//        } else {
//            hs.put("main_school", main_school.trim());
//            hs.put("sub_school", sub_school.trim());
//        }

        List<Student> students = studentService.getSupervise(hs);
        return Response.builder().status(STATUS.SUCCESS).total(studentService.getSystemSuperviseSum(hs)).data(students).build();
    }

    /*************************督学列表API*************************************************/

    /*
     * 获取当前考期学生
     * */
    @RequestMapping(value = "/students", method = RequestMethod.GET)
    public Response getSystemStudents(@RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("searchText") String searchText) {
        HashMap<String, Object> hs = new HashMap<>();
        int start = (page - 1) * limit;
        int total;
        hs.put("start", start);
        hs.put("pagesize", limit);
        if (!searchText.equals("")) {
            hs.put("exam_id", searchText.trim());
            hs.put("real_name", searchText.trim());
            hs.put("id_card", searchText.trim());
            hs.put("main_school", searchText.trim());
            hs.put("sub_school", searchText.trim());
        }
        Tag tag = tagService.getActiveTag();
        hs.put("tag_name", tag.getTag_name());
        List<Student> results = studentService.getSystemStudents(hs);
        total = (!searchText.equals("")) ? results.size() : studentService.getAllStuNumCurTag(hs);
        List<BigStudentForm> bigStudentForms = new ArrayList<>();

        results.forEach(student -> {
            BigStudentForm temp = BigStudentForm.builder().build();
            temp.setStu(student);
            temp.setCourses(stuCourseService.getById(student.getExam_id()));
            bigStudentForms.add(temp);
        });
        return Response.builder().status(STATUS.SUCCESS).total(total).data(bigStudentForms).build();

//        response.put("total", total);
//        response.put("data", bigStudentForms);
//        response.put("code", 20000);
//        return response;
    }

    /*
     * 获取历史考期数据
     * */
    @RequestMapping(value = "/students/tag", method = RequestMethod.GET)
    public Response getSystemStudentsByTag(@RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("tag") String tag) {

        Tag cur_tag = tagService.getActiveTag();
        if (cur_tag.getTag_name().equals(tag.trim())) {
            return this.getSystemStudents(limit, page, "");
        }

        HashMap<String, Object> hs = new HashMap<>();
        int start = (page - 1) * limit;
        int total;
        hs.put("start", start);
        hs.put("pagesize", limit);
        hs.put("tag_name", tag.trim());


        List<Student> results = studentService.getSystemStudentsByTag(hs);
        total = studentService.getAllStuNumByTag(hs);
        List<BigStudentForm> bigStudentForms = new ArrayList<>();

        results.forEach(student -> {
            BigStudentForm temp = BigStudentForm.builder().build();
            temp.setStu(student);
            temp.setCourses(stuCourseService.getByIdHis(student.getExam_id(), tag.trim()));
            bigStudentForms.add(temp);
        });
        return Response.builder().status(STATUS.SUCCESS).total(total).data(bigStudentForms).build();

//        response.put("total", total);
//        response.put("data", bigStudentForms);
//        response.put("code", 20000);
//        return response;
    }

    /*
     * 创建学生
     * */
    @RequestMapping(value = "/students", method = RequestMethod.POST)
    public Response createSystemStudent(@RequestBody JSONObject jsonObject) {
        Student student = JSONObject.parseObject(jsonObject.toJSONString(), Student.class);
        int code = studentService.insertSelective(student);
        return Response.builder().status(STATUS.SUCCESS).data(code).build();

//        response.put("data", code);
//        response.put("code", 20000);
//        return response;
    }

    /*
     * 更新学生
     * */
    @RequestMapping(value = "/students", method = RequestMethod.PUT)
    public Response updateSystemStudent(@RequestBody JSONObject jsonObject) {
        Student student = JSONObject.parseObject(jsonObject.toJSONString(), Student.class);
        int code = studentService.update(student);
        return Response.builder().status(STATUS.SUCCESS).data(code).build();

    }


    /*
     * 抓拍学生
     * */
    @RequestMapping(value = "/students/catch", method = RequestMethod.GET)
    public Response getStudentCourseCatch(@RequestParam("exam_id") String exam_id, @RequestParam("course_id") String course_id) {
        HashMap<String, Object> response = new HashMap<>();
        String phase = stuCourseService.getPhaseCatch(exam_id, course_id);
        String study = stuCourseService.getStudyCatch(exam_id, course_id);
        HashMap<String, String> data = new HashMap<>();
        if (null != study)
            data.put("study", study);
        else
            data.put("study", "");
        if (null != phase)
            data.put("phase", phase);
        else
            data.put("phase", "");
        return Response.builder().status(STATUS.SUCCESS).data(data).build();

//        response.put("data", data);
//        response.put("code", 20000);
//        return response;
    }

    /*
     * 考试试卷
     * */
    @RequestMapping(value = "/students/examination", method = RequestMethod.GET)
    public Response getStudentExamination(@RequestParam("exam_id") Long exam_id, @RequestParam("course_id") String course_id, @RequestParam("exam_type") Integer exam_type) {
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("exam_id", exam_id);
        hs.put("course_id", course_id.trim());
        hs.put("exam_type", exam_type);
        StuExam stuExam = stuExamService.getStuExamByParams(hs);
        if (null != stuExam)
            return Response.builder().status(STATUS.SUCCESS).data(stuExam).build();

//        response.put("data", stuExam);
        else
            return Response.builder().status(STATUS.BAD_REQUEST).message("无信息").build();

//            response.put("data", "");


    }

    /*
     * excel2json
     * 上传考卷
     * */
    @RequestMapping(value = "/excel2json", method = {RequestMethod.POST})
    public Response upload(@RequestParam("File") MultipartFile excelFile, HttpServletRequest request, HttpSession session) {
        try {
            String quiz = Quiz2JsonUtil.getTestJson(excelFile);
            return Response.builder().status(STATUS.SUCCESS).data(quiz).build();
//            response.put("code", 20000);
//            response.put("data", quiz);

        } catch (Exception e) {
            e.printStackTrace();
//            response.put("code", 400);
            return Response.builder().status(STATUS.BAD_REQUEST).message(e.getMessage()).build();
        }

//        return response;
    }

}
